package com.gai.shop.entity;


import com.gai.shop.util.DataBaseUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

public class Product implements Serializable {

  private Integer pid;
  private Integer tid;//值为1，实际上他就代表另外一条数据===>对应其他实体类
  private  Type type;//1 红米 小米手机，为发烧而生！Product.type.tname
  private String pname;
  private java.sql.Date ptime;
//  private java.util.Date ptimeForTest;//为了测试
  private String pimage;
  private double pprice;
  private Integer pstate;
  private String pinfo;
  //private  String tname;


//  public Date getPtimeForTest() {
//    return ptimeForTest;
//  }
//
//  public void setPtimeForTest(Date ptimeForTest) {
//    this.ptimeForTest = ptimeForTest;
//  }

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  public Product() {

  }

  public Integer getPid() {
    return pid;
  }

  public void setPid(Integer pid) {
    this.pid = pid;
  }


  public Integer getTid() {
    return tid;
  }

  public void setTid(Integer tid) {
    this.tid = tid;
  }


  public String getPname() {
    return pname;
  }

  public void setPname(String pname) {
    this.pname = pname;
  }


  public java.sql.Date getPtime() {
    return ptime;
  }

  public void setPtime(java.sql.Date ptime) {
    this.ptime = ptime;
  }


  public String getPimage() {
    return pimage;
  }

  public void setPimage(String pimage) {
    this.pimage = pimage;
  }


  public double getPprice() {
    return pprice;
  }

  public void setPprice(double pprice) {
    this.pprice = pprice;
  }


  public Integer getPstate() {
    return pstate;
  }

  public void setPstate(Integer pstate) {
    this.pstate = pstate;
  }


  public String getPinfo() {
    return pinfo;
  }

  public void setPinfo(String pinfo) {
    this.pinfo = pinfo;
  }

//  public static void main(String[] args) {
//    try {
//      String sql = "SELECT p.`pid`,p.`pname`,p.`pinfo`,p.`pprice`,p.`pstate`,t.`tname`,t.`tinfo`,t.`tid`" +
//              "FROM product p,`type` t WHERE p.`tid` = t.`tid` AND pid = 1;";
//      QueryRunner qr = new QueryRunner();
//      Product query = qr.query(DataBaseUtil.getConnection(),sql,new BeanHandler<>(Product.class));
//      System.out.println(query);
//    } catch (SQLException throwables) {
//      throwables.printStackTrace();
//    }
//  }

//    public static void main(String[] args) {
//    try {
//      String sql = "SELECT p.`pid`,p.`pname`,p.`pinfo`,p.`pprice`,p.`pstate`,t.`tname`,t.`tinfo`,t.`tid`" +
//              "FROM product p,`type` t WHERE p.`tid` = t.`tid` AND pid = 1;";
//      QueryRunner qr = new QueryRunner();
//      Map<String, Object> query = qr.query(DataBaseUtil.getConnection(), sql, new MapHandler());
//      //k=查询出来字段名/列名（注意可以被AS修改）  v=字段名/列名对应的数据
//      //声明了2个实体类的实例
//      Product p = new Product();
//      Type t = new Type();
//      //通过BeanUtils的populate进行数据的自动封装
//      BeanUtils.populate(p,query);
//      BeanUtils.populate(t,query);
//      //在product上设置type类型数据
//      p.setType(t);
//      System.out.println(p);
//      System.out.println("商品名:"+p.getPname() + "它属于：" + p.getType().getTname());
//    } catch (Exception throwables) {
//      throwables.printStackTrace();
//    }
//  }

}
